package com.zijidelu.luframework.domain.model.specification;

import com.zijidelu.luframework.base.model.DataModel;
import com.zijidelu.luframework.domain.model.aggregate.AggregateRoot;
import com.zijidelu.luframework.domain.model.entity.Entity;

/**
 * 抽象规约
 *
 * @param <A> 规约作用的对象类型
 * @author ZIJIDELU
 * @datetime 2025/10/30 16:47
 */
public interface Specification<A extends AggregateRoot<D, E>, E extends Entity<D>, D extends DataModel> {

    /**
     * 判断对象是否满足规约
     */
    boolean isSatisfiedBy(A candidate);

    /**
     * 与其他规约组合 - AND
     */
    default Specification<A, E, D> and(Specification<A, E, D> other) {
        return candidate -> this.isSatisfiedBy(candidate) && other.isSatisfiedBy(candidate);
    }

    /**
     * 与其他规约组合 - OR
     */
    default Specification<A, E, D> or(Specification<A, E, D> other) {
        return candidate -> this.isSatisfiedBy(candidate) || other.isSatisfiedBy(candidate);
    }

    /**
     * 取反
     */
    default Specification<A, E, D> not() {
        return candidate -> !this.isSatisfiedBy(candidate);
    }
}
